using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace OurWedding1.Models
{ 
    public class GuestRepository : IGuestRepository
    {
        readonly WeddingEntities _context = new WeddingEntities();

        public IQueryable<Guest> All
        {
            get { return _context.Guests; }
        }

        public IQueryable<Guest> AllIncluding(params Expression<Func<Guest, object>>[] includeProperties)
        {
            IQueryable<Guest> query = _context.Guests;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Guest Find(int id)
        {
            return _context.Guests.Find(id);
        }

        public void InsertOrUpdate(Guest guest)
        {
            if (guest.GuestId == default(int)) {
                // New entity
                _context.Guests.Add(guest);
            } else {
                // Existing entity
                _context.Entry(guest).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var guest = _context.Guests.Find(id);
            _context.Guests.Remove(guest);
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        public void Dispose() 
        {
            _context.Dispose();
        }
    }

    public interface IGuestRepository : IDisposable
    {
        IQueryable<Guest> All { get; }
        IQueryable<Guest> AllIncluding(params Expression<Func<Guest, object>>[] includeProperties);
        Guest Find(int id);
        void InsertOrUpdate(Guest guest);
        void Delete(int id);
        void Save();
    }
}